package com.byq.mm.smartlib.cluster.server;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.byq.mm.abstractx.engine.CoreEnvironment;
import com.byq.mm.application.cluster.datatype.SimpleLocation;
import com.byq.mm.application.cluster.server.LocationServer;
import com.byq.mm.application.cluster.server.executor.LocationExecutor;


public class AndroidLocationExecutor extends AndroidExecutor implements
		LocationExecutor {

	LocationListener locationListener;
	LocationServer ls;
	String provider = "network";

	public void start() {
		if (startted())
			return;
		locationListener = new LocationListener() {
			public void onLocationChanged(Location l) {

				update(l);

			}

			public void onProviderDisabled(String provider) {

			}

			public void onProviderEnabled(String provider) {

			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {

			}
		};

		LocationManager locationManager = (LocationManager) ctx
				.getSystemService(Context.LOCATION_SERVICE);
		if (provider.equals("network"))
			locationManager
					.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
							1000, 0, locationListener);
		else{
			
			
			
			locationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
		}
		Location location = locationManager
				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		update(location);

	}

	public void stop() {
		if (!startted())
			return;
		LocationManager locationManager = (LocationManager) ctx
				.getSystemService(Context.LOCATION_SERVICE);
		locationManager.removeUpdates(locationListener);
		locationListener = null;
	}

	public boolean startted() {
		return (locationListener != null);
	}

	public String getProvider() {
		return provider;
	}

	public void setProvider(Activity ac, String provider) {
		this.provider = provider;
		if(provider.equals("gps")){
			LocationManager locationManager = (LocationManager) ctx
			.getSystemService(Context.LOCATION_SERVICE);
			if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
				Toast.makeText(ac, "Please Open GPS", Toast.LENGTH_SHORT).show();   
		  //      Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);   
		  //      ac.startActivityForResult(intent,0); //此为设置完成后返回到获取界面   
	        }   
	  
	        

		}else{
			LocationManager locationManager = (LocationManager) ctx
			.getSystemService(Context.LOCATION_SERVICE);
			if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
				Toast.makeText(ac, "Please Open NETWORK", Toast.LENGTH_SHORT).show();   
		   //     Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);   
		     //   ac.startActivityForResult(intent,0); //此为设置完成后返回到获取界面   
	        }   
	  
		}
	}

	public AndroidLocationExecutor(String deviceId, String cluster,
								   CoreEnvironment env, Context ctx) {
		super(deviceId, cluster, env, ctx);

		ls = (LocationServer) env.getServerDeviceManager().getDeviceServer(
				deviceId).getClusterServer(cluster);

	}

	void update(Location l) {
		if (l != null) {
			Geocoder gc = new Geocoder(ctx);    
			        List<Address> addresses = null;   
			        try {   
			            addresses = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);   
			        } catch (IOException e) {   
			            // TODO Auto-generated catch block   
			            e.printStackTrace();   
			        }
			       

			SimpleLocation sl = new SimpleLocation();
			sl.setLatitude(l.getLatitude());
			sl.setLongitude(l.getLongitude());

			ls.onLocationChange(sl);

		}
	}

}
